﻿@{
    ViewBag.Title = "DeptShow";
}
<link href="~/Content/BootstrapSouce/css/bootstrap.css" rel="stylesheet" />
<script src="~/Content/BootstrapSouce/js/jquery.min.js"></script>
<script src="~/Content/BootstrapSouce/js/bootstrap.min.js"></script>
<script src="~/Content/EasyUI/jquery.min.js"></script>

<script>
    var url = "http://localhost:63805/apis";
    $(function () {
        $.ajax({
            type: "get",
            url: url+"/Department/DeptShow",
            success: function (obj) {
                $("#tds").empty();
                for (var i = 0; i < obj.length; i++) {
                    $("#tds").append("<tr><td>" + obj[i].department_Id + "</td><td>" + obj[i].department_Name + "</td><td>" + obj[i].department_Remarks + "</td><td>" + obj[i].department_IsStates + "</td><td><input type='button' value='启用' name='Qi" + i + "' onclick='Full(" + obj[i].department_Id + ")'  class='btn btn-success' /><input type='button' name='Ting" + i + "' value='停用' onclick='Stop(" + obj[i].department_Id + ")'  class='btn btn-success' /><input type='button' value='删除' onclick='Del(" + obj[i].department_Id + ")'  class='btn btn-success' /><input type='button' value='修改' onclick='Upt(" + obj[i].department_Id + ")'  class='btn btn-success'   data-toggle='modal' data-target='#modal_add_userUpts' /></td></tr>");
                    if (obj[i].department_IsStates == '已启用') {
                        $("[name='Qi" + i + "']").hide();
                        $("[name='Ting" + i + "']").show();
                    }
                    if (obj[i].department_IsStates == '未启用') {
                        $("[name='Qi" + i + "']").show();
                        $("[name='Ting" + i + "']").hide();
                    }
                }
            }
        })
    })
    $(function () {

        $.ajax({
            type: "get",
            url: url+"/Department/DeptCount",
            success: function (obj) {

                $("#lb1").html(obj);
            }
        })
    })
    function DeptAdd()
    {
        var department_Name = $("#Deptname").val();
        var department_Remarks = $("#DeptRemark").val();
        var department_IsState = $("input[type=radio][name=DeptAdd]:checked").val();
     
        $.ajax({
            type: "post",
            url: url+"/Department/DeptAdd",
            data: { department_Name: department_Name, department_Remarks: department_Remarks, department_IsState: department_IsState },
            success: function (obj)
            {
                if (obj > 0) {
                    alert("添加成功");
                    location.href = '/Department/DeptShow';
                }
                else {
                    alert("失败!");
                }
            }
        })

    }
    function Like() {
        var name = $("#likename").val();
        $.ajax({
            type: "get",
            url: url+"/Department/DeptLike?name=" + name,
            success: function (obj) {
                $("#tds").empty();
                for (var i = 0; i < obj.length; i++) {
                    $("#tds").append("<tr><td>" + obj[i].department_Id + "</td><td>" + obj[i].department_Name + "</td><td>" + obj[i].department_Remarks + "</td><td>" + obj[i].department_IsStates + "</td><td><input type='button' value='启用' name='Qi" + i + "' onclick='Full(" + obj[i].department_Id + ")'  class='btn btn-success' /><input type='button' name='Ting" + i + "' value='停用' onclick='Stop(" + obj[i].department_Id + ")'  class='btn btn-success' /><input type='button' value='删除' onclick='Del(" + obj[i].department_Id + ")'  class='btn btn-success' /><input type='button' value='修改' onclick='Upt(" + obj[i].department_Id + ")'  class='btn btn-success'   data-toggle='modal' data-target='#modal_add_userUpts' /></td></tr>");
                    if (obj[i].department_IsStates == '已启用') {
                        $("[name='Qi" + i + "']").hide();
                        $("[name='Ting" + i + "']").show();
                    }
                    if (obj[i].department_IsStates == '未启用') {
                        $("[name='Qi" + i + "']").show();
                        $("[name='Ting" + i + "']").hide();
                    }
                }
            }
        })
    }
    function ShowAll() {
        location.href = '/Department/DeptShow';
    }

    function Full(id)
    {
        $.ajax({
            type: "post",
            url: url+"/Department/DeptFull?id=" + id,
            success: function (obj) {
                if (obj > 0) {
                    alert("启用成功,此账号可以使用");
                    location.href = '/Department/DeptShow';
                }
                else {
                    alert("失败!");
                }
            }
        })
    }

    function Stop(id) {

        $.ajax({
            type: "post",
            url: url+"/Department/DeptStop?id=" + id,
            success: function (obj) {
                if (obj > 0) {
                    alert("停用成功,此账号不可以使用");
                    location.href = '/Department/DeptShow';
                }
                else {
                    alert("失败!");
                }
            }
        })
    }

    function Del(id)
    {
        $.ajax({
            type: "post",
            url: url+"/Department/DeptDel?id=" + id,
            success: function (obj)
            {
                if (obj > 0)
                {
                    alert("删除成功");
                    location.href = '/Department/DeptShow';
                    }
                else
                {
                    alert("失败!");
                }
            }
        })
    }

    function Upt(id) {
        $.ajax({
            type: "get",
            url: url+"/Department/DeptSelectId?id=" + id,
            success: function (obj) {
                $("#department_Id").val(obj[0].department_Id);
                $("#department_Name").val(obj[0].department_Name);
                $("#department_Remarks").val(obj[0].department_Remarks);


            }
        })

        function UptFull() {
            var department_Id = $("#department_Id").val();
            var department_Name = $("#department_Name").val();
            var department_Remarks = $("#department_Remarks").val()

            $.ajax({
                type: "post",
                url: url+"/Department/DeptUptFull",
                data: { department_Id: department_Id, department_Name: department_Name, department_Remarks: department_Remarks },
                success: function (obj) {
                    if (obj > 0) {
                        alert("修改成功");
                        location.href = '/Department/DeptShow';

                    }
                    else {
                        alert("失败!");
                    }
                }
            })
        }
    }
</script>
<style>
    #lb2 {
        float: right;
    }
</style>


<input type="button" value="添加部门" class="btn btn-success" data-toggle="modal" data-target="#modal_add_user" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="text" id="likename" /><input type="button" value="根据名字来查询" class="btn btn-success" onclick="Like()" /><input type="button" value="显示全部" onclick="ShowAll()" class="btn btn-success" />  <span id="lb2" style="font-size:30">共有<span id="lb1" style="color:red"></span>个部门</span>
<table class="table table-striped">
    <tr>
        <td>部门编号</td>
        <td>部门名称</td>
        <td>部门备注</td>
        <td>启用状态</td>
        <td>操作</td>

    </tr>
    <tbody id="tds"></tbody>
</table>



<div class="modal fade" id="modal_add_user" tabindex="-1" role="dialog" aria-labelledby="label_course">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="label_course"><strong>添加部门</strong></h4>
            </div>
            <div class="modal-body">
                <table class="table table-striped">
                    <tr>
                        <td>部门名称</td>
                        <td><input type="text" id="Deptname" /></td>
                    </tr>
                    <tr>
                        <td>部门备注</td>
                        <td><input type="text" id="Name" /></td>
                    </tr>
                    <tr>
                        <td>是否启用</td>
                        <td><input type="radio" name="DeptAdd" value="0" />未启用<input type="radio"  value="1" name="DeptAdd"/>启用</td>
                    </tr>
                </table>
                <input type="button" value="添加部门" onclick="DeptAdd()"  class="btn btn-success"/>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                </div>
            </div>
        </div>
    </div>
</div>



<div class="modal fade" id="modal_add_userUpts" tabindex="-1" role="dialog" aria-labelledby="label_course">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="label_course"><strong>修改部门信息</strong></h4>
            </div>
            <div class="modal-body">
                <table class="table table-striped">
                    <tr>
                        <td>部门编号</td>
                        <td><input type="text" id="department_Id" disabled /></td>
                    </tr>
                    <tr>
                        <td>部门姓名</td>
                        <td><input type="text" id="department_Name" /></td>
                    </tr>

                    <tr>
                        <td>部门备注</td>
                        <td><input type="text" id="department_Remarks" /></td>
                    </tr>

                </table>
                <input type="button" value="修改" onclick="UptFull()" />
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                </div>
            </div>
        </div>
    </div>
</div>
